package vue.edition;

import javax.swing.JPanel;
import javax.swing.BoxLayout;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;

import java.awt.FlowLayout;

import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Font;

import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JList;
import javax.swing.border.LineBorder;

import java.awt.SystemColor;
import java.awt.Component;

import javax.swing.SwingConstants;

import java.awt.CardLayout;
import java.awt.Dimension;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextArea;

import controler.Lieu;
import controler.edition.EditLieu;
import controler.edition.EditNews;

import java.awt.Container;

import javax.swing.JFrame;

public class GererLieu extends JPanel {
	
	private JPanel contentPane;
	private JPanel panelRightBot;
	private JPanel PanelBase;
	private JPanel panelLeft;
	private JPanel panelLeftTop;
	private JPanel panelLeftBot;
	private JPanel panelLeftMid;
	private JPanel panelRight;
	private JPanel panelRightMid;
	private JPanel panelTypeSearch;
	private JLabel lblRechercher;
	private JTextField textFieldSearch;
	private JPanel panelSearch;
	private JRadioButton rdbtnParMotCl;
	private JRadioButton rdbtnParNomExact;
	private ButtonGroup grouprdbtn;
	private JList<String> listLieu;
	private JButton btnRechercher;
	private JButton btnActualiser;
	private JButton btnModifier;
	private JButton btnSupprimer;
	private JButton btnAjouter;
	private JPanel panelAddLib;
	private JPanel panelAddLblDes;
	private JPanel panelAddImg;
	private JLabel lblLibell;
	private JTextField textFieldLib;
	private JLabel lblDescription;
	private JPanel panelAddDes;
	private JTextArea textArea;
	private JPanel panelAddImgTxt;
	private JPanel panelAddImgBrowse;
	private JTextField textField;
	private JButton btnSlectionnerImage;


	public GererLieu(JPanel contentPane) {
		this.contentPane = contentPane;
	}
	
	public void initComponents() {
		PanelBase = new JPanel();
		PanelBase.setBorder(new EmptyBorder(0, 10, 0, 10));
		contentPane.add(PanelBase);
		PanelBase.setLayout(new BoxLayout(PanelBase, BoxLayout.X_AXIS));
		
		panelLeft = new JPanel();
		panelLeft.setBorder(new TitledBorder(null, "G\u00E9rer les Lieux", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		PanelBase.add(panelLeft);
		panelLeft.setLayout(new BorderLayout(0, 0));
		
		panelLeftTop = new JPanel();
		panelLeft.add(panelLeftTop, BorderLayout.NORTH);
		panelLeftTop.setLayout(new BoxLayout(panelLeftTop, BoxLayout.Y_AXIS));
		
		panelSearch = new JPanel();
		panelLeftTop.add(panelSearch);
		
		lblRechercher = new JLabel("Rechercher : ");
		panelSearch.add(lblRechercher);
		
		textFieldSearch = new JTextField();
		textFieldSearch.setHorizontalAlignment(SwingConstants.LEFT);
		panelSearch.add(textFieldSearch);
		textFieldSearch.setColumns(10);
		
		btnRechercher = new JButton("OK");
		panelSearch.add(btnRechercher);
		
		panelTypeSearch = new JPanel();
		panelLeftTop.add(panelTypeSearch);
		
		rdbtnParMotCl = new JRadioButton("Par Mots Cl\u00E9s");
		rdbtnParMotCl.setSelected(true);
		panelTypeSearch.add(rdbtnParMotCl);
		
		rdbtnParNomExact = new JRadioButton("Par Nom Exact");
		panelTypeSearch.add(rdbtnParNomExact);

		grouprdbtn = new ButtonGroup();
		grouprdbtn.add(rdbtnParMotCl);
		grouprdbtn.add(rdbtnParNomExact);
		
		panelLeftMid = new JPanel();
		panelLeft.add(panelLeftMid, BorderLayout.CENTER);
		panelLeftMid.setLayout(new BorderLayout(0, 0));
		
		listLieu = new JList<String>(new EditLieu().listerAll());
		panelLeftMid.add(listLieu, BorderLayout.CENTER);
		
		panelLeftBot = new JPanel();
		panelLeft.add(panelLeftBot, BorderLayout.SOUTH);
		
		btnActualiser = new JButton("Actualiser");
		panelLeftBot.add(btnActualiser);
		
		btnModifier = new JButton("Modifier");
		panelLeftBot.add(btnModifier);
		
		btnSupprimer = new JButton("Supprimer");
		panelLeftBot.add(btnSupprimer);
		
		panelRight = new JPanel();
		panelRight.setBorder(new TitledBorder(null, "Ajouter un Lieu", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		PanelBase.add(panelRight);
		panelRight.setLayout(new BorderLayout(0, 0));
		
		panelRightMid = new JPanel();
		panelRight.add(panelRightMid, BorderLayout.CENTER);
		panelRightMid.setLayout(new BoxLayout(panelRightMid, BoxLayout.Y_AXIS));
		
		panelAddLib = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panelAddLib.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panelAddLib.setMaximumSize(new Dimension(32767, 50));
		panelRightMid.add(panelAddLib);
		
		lblLibell = new JLabel("Libell\u00E9 :");
		panelAddLib.add(lblLibell);
		
		textFieldLib = new JTextField();
		panelAddLib.add(textFieldLib);
		textFieldLib.setColumns(10);
		
		panelAddLblDes = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panelAddLblDes.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panelAddLblDes.setMaximumSize(new Dimension(32767, 50));
		panelRightMid.add(panelAddLblDes);
		
		lblDescription = new JLabel("Description :");
		panelAddLblDes.add(lblDescription);
		
		panelAddDes = new JPanel();
		panelRightMid.add(panelAddDes);
		panelAddDes.setLayout(new BoxLayout(panelAddDes, BoxLayout.X_AXIS));
		
		textArea = new JTextArea();
		panelAddDes.add(textArea);
		
		panelAddImg = new JPanel();
		panelRightMid.add(panelAddImg);
		panelAddImg.setLayout(new BoxLayout(panelAddImg, BoxLayout.X_AXIS));
		
		panelAddImgTxt = new JPanel();
		panelAddImgTxt.setMaximumSize(new Dimension(32767, 25));
		panelAddImg.add(panelAddImgTxt);
		panelAddImgTxt.setLayout(new BoxLayout(panelAddImgTxt, BoxLayout.X_AXIS));
		
		textField = new JTextField();
		panelAddImgTxt.add(textField);
		textField.setColumns(10);
		
		panelAddImgBrowse = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) panelAddImgBrowse.getLayout();
		flowLayout_2.setAlignment(FlowLayout.LEFT);
		panelAddImg.add(panelAddImgBrowse);
		
		btnSlectionnerImage = new JButton("S\u00E9lectionner Image");
		panelAddImgBrowse.add(btnSlectionnerImage);
		
		panelRightBot = new JPanel();
		panelRight.add(panelRightBot, BorderLayout.SOUTH);
		
		btnAjouter = new JButton("Ajouter");
		panelRightBot.add(btnAjouter);
		
		listener();
	}

	public void refreshList() {
		contentPane.setVisible(false);
		contentPane.removeAll();
		initComponents();
		contentPane.setVisible(true);
	}
	
	public void listener() {

		btnRechercher.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(rdbtnParMotCl.isSelected()) {
					listLieu.setModel(new EditLieu().searchKeyWord(textFieldSearch.getText()));
				} else if(rdbtnParNomExact.isSelected()) {
					listLieu.setModel(new EditLieu().searchExactWord(textFieldSearch.getText()));
				}
			}
		});
		
		btnActualiser.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				refreshList();
			}
		});
		
		btnModifier.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (listLieu.getSelectedValue() != null) {
					String[] split = listLieu.getSelectedValue().split(" | ");
					int id = Integer.parseInt(split[0]);
					
					ModificationPopup frame = new ModificationPopup();
					frame.initLieu(new Lieu(id));
					frame.setVisible(true);
				}
			}
		});
		
		btnSupprimer.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (listLieu.getSelectedValue() != null) {
					String[] split = listLieu.getSelectedValue().split(" | ");
					int id = Integer.parseInt(split[0]);
					
					new EditLieu().delete(id);
					refreshList();
				}
			}
		});
		
		btnAjouter.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new EditLieu().ajouterLieu(textFieldLib.getText(), textArea.getText(), textField.getText());
				textFieldLib.setText(null);
				textField.setText(null);
				textArea.setText(null);
				refreshList();
			}
		});
	
		btnSlectionnerImage.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser c = new JFileChooser();
				// Demonstrate "Open" dialog:
				int rVal = c.showOpenDialog(GererLieu.this);
				if (rVal == JFileChooser.APPROVE_OPTION) {
					textField.setText(c.getCurrentDirectory().toString() + "\\" + c.getSelectedFile().getName());
				}
				if (rVal == JFileChooser.CANCEL_OPTION) {
					textField.setText("");
				}
			}
		});
	}
}
